jQuery(function () {
    var $ = jQuery, // just in case. Make sure it's not an other libaray.

        $wrap = $('#uploader'),

        // 图片容器
        $queue = $('<ul class="filelist"></ul>')
            .appendTo($wrap.find('.queueList')),

        // 状态栏，包括进度和控制按钮
        $statusBar = $wrap.find('.statusBar'),

        // 文件总体选择信息。
        $info = $statusBar.find('.info'),

        // 上传按钮
        $upload = $wrap.find('.uploadBtn'),

        // 没选择文件之前的内容。
        $placeHolder = $wrap.find('.placeholder'),

        // 总体进度条
        $progress = $statusBar.find('.progress').hide(),

        // 添加的文件数量
        fileCount = 0,

        // 添加的文件总大小
        fileSize = 0,

        // 优化retina, 在retina下这个值是2
        ratio = window.devicePixelRatio || 1,

        // 缩略图大小
        thumbnailWidth = 110 * ratio,
        thumbnailHeight = 110 * ratio,

        // 可能有pedding, ready, uploading, confirm, done.
        state = 'pedding',

        // 所有文件的进度信息，key为file id
        percentages = {},

        supportTransition = (function () {
            var s = document.createElement('p').style,
                r = 'transition' in s ||
                    'WebkitTransition' in s ||
                    'MozTransition' in s ||
                    'msTransition' in s ||
                    'OTransition' in s;
            s = null;
            return r;
        })(),

        // WebUploader实例
        uploader;

    if (!WebUploader.Uploader.support()) {
        alert('Web Uploader 不支持您的浏览器！如果你使用的是IE浏览器，请尝试升级 flash 播放器');
        throw new Error('WebUploader does not support the browser you are using.');
    }

    // 实例化
    uploader = WebUploader.create({
        pick: {
            id: "#filePicker",
            label: "点击选择文件"
        },
        dnd: '#uploader .queueList',
        paste: document.body,
        accept: {
            title: "bpmn files",
            extensions: "bpmn,bpmn20.xml",
            mimeTypes: "application/xml"
        },
        swf: $ctx + "/assets/js/plugins/webuploader/Uploader.swf",
        auto: true,
        fileVal: "file",
        disableGlobalDnd: true,
        chunked: true,
        server: $ctx + "/flow/models/upload",
        fileNumLimit: 1,
        fileSizeLimit: 5 * 1024 * 1024, // 200 M
        fileSingleSizeLimit: 1 * 1024 * 1024// 50 M
    });

    uploader.on("uploadSuccess", function (file, response) {
        setProgress("100%");
        var data = response.data;
        var index = parent.layer.getFrameIndex(window.name);
        parent.layer.close(index);
        parent.location.href = $ctx + "/activiti/editor/#/editor/" + data.id;
    });

    uploader.onError = function (code) {
        alert('Error: ' + code);
    };

    uploader.on("uploadStart", function (file) {
        $("#progressBar:hidden").show();
    });

    uploader.on("uploadProgress", function (file, percentage) {
        setProgress(percentage);
    });

    setProgress = function (percentage) {
        $("#progressBar .progress-bar").width(percentage);
        $("#progressBar .sr-only").text(percentage + " Complete");
    }
});